import { System } from "./System";
import { ComponentType } from "../component/Component";
import { Register } from "../register/Register";
/**
 * @class
 * @extends w.system.System
 * @memberof w.system
 */
class TransSystem extends System {
  constructor(opts = {}) {
    super(opts);
  }
  run(event) {
    if (event.componentType !== ComponentType.Trans) {
      return;
    }
    //获取trans对象可能关联的组件。
    const trans = event.target;
    trans.eids.forEach(eid => {
      const entity = Register.instance.entityManager.get(eid);
      this.transerUpdate(entity);
    });
  }
  transerUpdate(entity) {
    let mesh = entity.getComponent(ComponentType.Mesh);
    if (mesh) {
      mesh.update();
    }
    let camera = entity.getComponent(ComponentType.Camera);
    if (camera) {
      camera.setState(false, "vp");
      camera.update();
    }

    for (let index = 0; index < entity.children.length; index++) {
      const eid = entity.children[index];
      let _entity = Register.instance.entityManager.get(eid);
      if(_entity){
        this.transerUpdate(_entity);
      }
    }
  }
}
export { TransSystem };
